﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Domain
{
    public abstract class Entity : IEntity
    {
        protected Entity()
        {
        }

        protected Entity(string id)
        {
            Id = id;
        }

        public string Id { get; private set; }

        /// <summary>
        ///     Check if this entity is transient, ie, without identity at this moment
        /// </summary>
        /// <returns>True if entity is transient, else false</returns>
        public bool IsTransient()
        {
            return string.IsNullOrEmpty(Id);
        }

        /// <summary>
        ///     Generate identity for this entity
        /// </summary>
        public void GenerateNewIdentity()
        {
            if (IsTransient())
                Id = IdentityGeneratorFactory.Instance.NewId();
        }

        /// <summary>
        ///     Change current identity for a new non transient identity
        /// </summary>
        /// <param name="identity">the new identity</param>
        public void ChangeCurrentIdentity(string identity)
        {
            if (identity != string.Empty)
                Id = identity;
        }
    }
}
